﻿using Assets.AnimationPlayer.Scripts.Util;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
using Assets.AnimationPlayer.Scripts.Json;

namespace Assets.AnimationPlayer.Scripts.UI
{
    public class UnDestoryScene:MonoBehaviour
    {
        public ArApi CallApi;
        internal void Awake()
        {
            DontDestroyOnLoad(this);
            AnimationVar.UnDestoryScene = this;
            AnimationVar.ScaleNum = Mathf.Max(Screen.dpi / 401.0f, 0.5f);
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                CallApi.AutoPlayModel = true;
                AnimationVar.AnimationPlayCfg.AutoPlay = true;
            }
            else
            {
                CallApi.AutoPlayModel = false;
            }

            SceneManager.LoadScene("AnimationPlayer/Scene/AnimationListView",LoadSceneMode.Single);
        }

        int times = 0;
        bool _isLoading;
        internal void FixedUpdate()
        {
            if (!ArApi.ApiInit) return;
            if (CallApi.AutoPlayModel) return;
            if (_isLoading) return;
            times++;
            if (times > 50)
            {
                _isLoading = true;
                times = 0;
                StartCoroutine(GetAnimationName());
            }
        }

        private  IEnumerator GetAnimationName()
        {
            yield return null;
            if(Application.internetReachability != NetworkReachability.NotReachable)
            {
                Debug.Log("has network");
                WWW www = new WWW(@"http://192.168.31.46:33388/yioks_admin.mpl?a=u3d_name_get");
                yield return www;
                if (www.error != null)
                {
                    Debug.Log(www.error);
                }
                else
                {
                    string result = Encoding.UTF8.GetString(www.bytes);
                    Debug.Log("result:" + result);
                    try
                    {
                        Dictionary<string, object> resultDic = (Dictionary<string, object>)Json.Json.Deserialize(result);
                        string status = (string)resultDic["status"];
                         Debug.Log("status:"+status);
                        if (status == "ok")
                        {
                            Dictionary<string, object> animationDic = (Dictionary<string, object>)resultDic["info"];
                            string animationName = (string)animationDic["value"];
                            if (!string.IsNullOrEmpty(animationName))
                            {
                                CallApi.UNITY_set_animation(animationName);
                            }
                            else
                            {
                                CallApi.UNITY_set_animation("");
                            }
                        }
                    }
                    catch
                    {
                        CallApi.UNITY_set_animation("");
                    }
                }
            }
            else
            {
                Debug.Log("no network");
            }
            _isLoading = false;
        }
    }
}
